package com.weijian.dal.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
 * 城市枚举类
 */
@Getter
public enum City {
    CHENGDU(0, "成都", 104.0668, 30.5728),
    CHONGQING(1, "重庆", 106.5516, 29.5630),
    GUIYANG(2, "贵阳", 106.7074, 26.5982),
    HUAIHUA(3, "怀化", 110.0016, 27.5501),
    BEIBUWAN(4, "北部湾", 108.3277, 21.9733),
    SHANGHAI(5, "上海", 121.4737, 31.2304),
    HO_CHI_MINH(6, "胡志明", 106.6951, 10.8231),
    BANGKOK(7, "曼谷", 100.5018, 13.7563),
    SINGAPORE(8, "新加坡", 103.8198, 1.3521);

    @EnumValue
    private final int code;

    private final String name;

    private final double longitude;

    private final double latitude;

    City(int code, String name, double longitude, double latitude) {
        this.code = code;
        this.name = name;
        this.longitude = longitude;
        this.latitude = latitude;
    }

    /**
     * 根据编码获取城市
     *
     * @param code 城市编码
     * @return 城市枚举，如果未找到返回null
     */
    public static City fromCode(int code) {
        for (City city : City.values()) {
            if (city.getCode() == code) {
                return city;
            }
        }
        return null;
    }


    @JsonValue
    public CityInfo getInfo() {
        return new CityInfo(this.name, this.longitude, this.latitude);
    }

    // 内部类用于序列化
    @Getter
    public static class CityInfo {
        private final String name;
        private final double longitude;
        private final double latitude;

        public CityInfo(String name, double longitude, double latitude) {
            this.name = name;
            this.longitude = longitude;
            this.latitude = latitude;
        }

        @Override
        public String toString() {
            return "CityInfo{name='" + name + "', longitude=" + longitude + ", latitude=" + latitude + "}";
        }
    }
}